<?php
/**
 * This class manages user address book information items and groups
 */
class Zend_Jabber_AddressBook {
    /**
     * Address book owner
     *
     * @var Zend_Jabber_User
     */
    private $owner;
    
    /**
     * Array of address book items
     *
     * @see Zend_Jabber_AddressBookItem
     * @var array
     */
    private $groups;
    
    /**
     * Create new address book object object
     * 
     * @param Zend_Jabber_User Addres book owner
     */
    public function __construct(Zend_Jabber_User $owner) {
        $this->owner = $owner;
        $this->addGroup(new Zend_Jabber_AddressBook_Group($this, 'Buddies', true));
    }

    /**
     * Add new group to address book
     * 
     * @param Zend_Jabber_AddressBook_Group $group Address book group
     */
    public function addGroup(Zend_Jabber_AddressBook_Group $group) {
        $this->groups[$group->getName()] = $group;
    }
	
    /**
     * Remove group from address book
     * 
     * This method does not effect server copy of address book, because groups
     * are stored as user keywords on server. 
     * 
     * @param Zend_Jabber_AddressBook_Group $group Roster group
     */
    public function removeGroup(Zend_Jabber_AddressBook_Group $group) {
        unset($this->groups[$group->getName()]);
    }
    
    /**
     * Get available available groups
     * 
     * @return array
     */
    public function getGroups() {
        return $this->groups;
    }
    
    /**
     * Get default address book group
     * 
     * @return Zend_Jabber_AddressBook_Group
     */
    public function getDefaultGroup() {
        $groups = $this->getGroups();
        foreach($groups as $group) {
            if($group->isDefault()) return $group; 
        }
        
        return isset($groups[0]) ? $groups[0] : false;
    }
        
    /**
     * Get group by name
     * 
     * @param string $name Group name
     * @return Zend_Jabber_AddressBook_Group
     */
    public function getGroupByName($name) {
        return isset($this->groups[$name]) ? $this->groups[$name] : false;
    }

    /**
     * Get address book owner
     * 
     * @return Zend_Jabber_User
     */
    public function getOwner() {
        return $this->owner;
    }

    /**
     * Get array of users in the address book
     * 
     * @return array
     */
    public function getUsers() {
        $users = array();
        foreach($this->getItems() as $item) {
            $users[] = $item->getUser();
        }
        
        return $users;
    }
	
    /**
	 * Get user object by specifiend user JID 
	 * 
	 * If there is no user with such jid in the address book then false will be returned
	 * 
	 * @return Zend_Jabber_User
	 */
	public function getUserByJid($jid) {
	    $jid = preg_replace('/\/.*/', '', $jid);
	    
	    foreach($this->getUsers() as $user) {
	        if($user->getJid(false) == $jid) return $user;
	    }
	    
	    return false;
	}
    
    /**
     * Get all contacts (items) in this address book
     * 
     * @return array
     */
    public function getItems() {
        $items = array();
        
        foreach($this->getGroups() as $group) {
            foreach($group->getItems() as $item) {
                $items[$item->getUser()->getJid(false)] = $item;
            }
        }
        
        return array_values($items);
    }
        	
	/**
	 * Remove item from address book
	 * 
	 * @param Zend_Jabber_AddressBook_Item $item
	 */
	public function removeItem(Zend_Jabber_AddressBook_Item $item) {
	    foreach($this->getGroups() as $group) {
	        $group->removeItem($item);
	    }
	}
    
    /**
     * Conver roster object to a string
     * 
     * You can use this function for debuging as it presents user readable information 
     * about user's address book
     * 
     * @return string
     */
    public function toString() { 
	    $stringPresentation = "";
        foreach($this->getGroups() as $group) {
		    $stringPresentation .= $group->getName() . "\n";
		    foreach($group->getItems() as $Item) {
		        $status = in_array($Item->getSubscription(), array('to', 'both')) ? $Item->getUser()->getShow() : 'unsubscribed'; 
		        
		        $stringPresentation .= "    " . $Item->getUser()->getJid(false) . ' - ' . $status . "\n";
		    }
        }
        
	    return $stringPresentation;
	}

    /**
     * Print roster object
     * 
     * @return string
     */
	public function __toString() {
	    return $this->toString();
	}
}
?>